#!/bin/bash

MYSQLDUMP=$HOME/.local/mysql-5.7.24/bin/mysqldump
MYSQL=$HOME/.local/mysql-5.7.24/bin/mysql

PWD=$(cd $(dirname $0); pwd)
LANG=en_US.UTF-8
DATETIME=$(date "+%Y%m%d%H%M%S")
DATE=${DATETIME:0:8}
BACKUP_DIR=$PWD/$DATE
if [ ! -d "$BACKUP_DIR" ];then
    mkdir $BACKUP_DIR
fi
FILENAME="$BACKUP_DIR/online-backup-$DATETIME.sql"
ONLINE_CONNECT="${MYSQLDUMP} -h $host -P $port -u $user $db_name --default-character-set=utf8 --single-transaction --set-gtid-purged=OFF -p$password"
RD_CONNECT="${MYSQL} -h localhost -P 8306 -u $user $db_name --default-character-set=utf8"

echo "$ONLINE_CONNECT" | bash > $FILENAME

sed '/SET @@GLOBAL.GTID_PURGED/,+2d' $FILENAME -i

echo "${RD_CONNECT} < ${FILENAME}" | bash

gzip $FILENAME
